<?php
vendor('dBug');
/****
* 用户账户模块, 处理 注册\登录\注销 等相关事宜
****/
class AccountAction extends InitAction{
    protected function _initialize(){
        parent::_initialize();
    }
    public function index(){
        $this->profile();
    }
    public function register(){
        if ($this->isLogged()){
        //登录状态下不允许注册
            $this->redirect('index/');
            return;
        }
        
        $AM= D('Account');
        if (! isset($_POST['submit'])){
            $this->display();
            return;
        } else{
            $this->setJump();
            $data= $this->wrapRequest($_POST);
            $data['name']= $data['username'];
            if ($vo=$AM->create($data)){
                $vo= $vo+ $data;    //避免覆盖$vo的值
                if ($uid=$AM->register($vo)){
                    $vo['uid']= $uid;
                    $this->_afterLogin($vo);
                    $this->success("注册成功");
                } else{
                    $this->error($AM->getError());
                }
            } else{
                $this->error($AM->getError());
            }
        }
    }
    public function login(){
        if ($this->isLogged()){
        //已经登录过了
            $this->redirect('index/');
            return;
        }
        
        $AM= D('Account');
        $this->setJump();
        if (isset($_POST['submit'])){
            //TODO: login check            
            $u= parent::login($_POST['username'], $_POST['password']);
            if ($u[0] >0){
                $this->success("登录成功" );
            } else{
                if (parent::C('LOGIN_ERROR_USERNAME')==$u[0] ||parent::C('LOGIN_ERROR_PASSWORD')==$u[0]){
                    $this->error("用户名或密码错误");
                } else if (parent::C('LOGIN_ERROR_ANSWER')==$u[0]){
                    $this->error("安全提问错误");
                } else{
                    $this->error("请重新登录");
                }
            }
        } else{
            $this->display();
        }
    }
    public function logout(){
        if (parent::logout($_GET['ck'])){
            $this->setJump();
            $this->success("已安全注销");
        } else{
            $this->redirect('index/');
        }
    }
    /****
    * 修改基本资料
    ****/
    public function profile(){
        $uid= $this->isLogged();
        if (! $uid){
        //用户还未登录
            $this->redirect('login');
            return;
        }
        $am=D('Account');
        if (isset($_POST['submit'])){
            if (false===$am->edit($uid, $_POST)){
                $this->error($am->getError());
            }
            $this->success("修改成功");
        } else{
            $u= $am->getByID($uid);
            
            $this->assign('user', $u);
            $this->assign('content', $this->fetch('profile.mod'));
            $this->display('setting');
        }
    }
    /****
    * 修改密码
    */
    public function password(){
        $uid= $this->isLogged();
        if (! $uid){
            $this->redirect('login');
            return;
        }
        
        if (isset($_POST['submit'])){
            $am=D('Account');
            if (false===$am->edit($uid, $_POST)){
                $this->error($am->getError());
            }
            $this->setJump();
            $this->success("修改成功");
            //TODO: 密码修改后要求重新登录
        } else{
            //必须要有获取username
            //TODO 修改ucenter或者Account以替代
            $am=new AccountModel();
            $u= $am->getByID($uid);
            
            $this->assign('user', $u);
            $this->assign('content', $this->fetch('password.mod'));
            $this->display('setting');
        }
    }
    /****
    * 修改头像
    */
    public function avatar(){
        $this->checkLogged();
        //$this->assign('user', $u);
        $this->assign('content', $this->fetch('avatar.mod'));
        $this->display('setting');
    }
    /****
    * 修改介绍, 仅供社团用户使用
    */
    public function intro(){
        $u= $this->checkLogged();
        if (AccountModel::C('ROLE_GROUP') !=$u['role']){
            $this->error("没有权限访问该页面");
        }
        if ($_POST['submit']){
        
            $this->assign('content', $this->fetch('intro.edit.mod'));
            $this->assign("jumpUrl", __ACTION__);
            $this->success("修改成功");
        } else{
            //$this->assign('user', $u);
            $this->assign('content', $this->fetch('intro.edit.mod'));
            $this->display('setting');
        }
    }
    protected function checkLogged(){
        $u= $this->getInfo();
        if (! $u){
            $this->redirect('login');
            return false;
        }
        return $u;
    }
    protected function setJump(){
        $jumpUrl= $_GET['refer'];
        if (! $jumpUrl){
            $jumpUrl= $_SERVER['HTTP_REFERER'];
        }
        if (! $jumpUrl){
            $jumpUrl= U('index');
        }
        $this->assign("jumpUrl", $jumpUrl);
    }
    protected function wrapRequest($raw =null){
        if (is_null($raw)){
            $raw= $_POST;
        }
        $reqMap= array(
            'nickname'   =>'name',
        );
        $res= $raw;
        Notice::Remap($res, $reqMap);
        return $res;
    }
}
?>